﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using PalBattleEmulator.Battle;

namespace PalBattleEmulator.Graphic
{
    /// <summary>
    /// BattleFieldLayerControl.xaml 的交互逻辑
    /// </summary>
    public partial class BattleFieldLayer : UserControl
    {
        
        private ImageSource battleFieldImage;

        public BattleFieldLayer()
        {
            InitializeComponent();
            this.BeginInit();
            this.Width = GlobalConfig.GRAPHIC_WIDTH;
            this.Height = GlobalConfig.GRAPHIC_HEIGHT;
            this.EndInit();
        }

        public void BattleInitialize(GameBattle battle)
        {
            this.Dispatcher.Invoke(new setImageDelegate(setBattleFieldImage), battle.BattleField.Id);
        }

        public void Redraw()
        {
            this.Dispatcher.Invoke(new drawBattleFieldDelegate(drawBattleField));
        }

        #region delegate methods

        private delegate void drawBattleFieldDelegate();
        private delegate void setImageDelegate(int id);

        private void setBattleFieldImage(int id)
        {
            battleFieldImage = ContextModules.BattleFieldImageManager.GetImage(id);
        }

        private void drawBattleField()
        {
            if(image.ImageSource != battleFieldImage)
                this.image.ImageSource = battleFieldImage;
        }

        #endregion
    }
}
